package cn.q3c.stock.data;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import cn.q3c.stock.object.LogCJ;
import cn.q3c.stock.object.Stock;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;
import org.xBaseJ.DBF;
import org.xBaseJ.xBaseJException;

/**
 * 解析证交所DBF文件
 * 
 * @author Yoyo
 * 
 */
public class DBFUtils {

	public static List<Stock> toStockList(File file) throws xBaseJException,
			IOException {
		String fileName = file.toString();
		List<Stock> retList = new ArrayList<Stock>();
		DBF dbf = new DBF(fileName, "gbk");

		if (StringUtils.containsIgnoreCase(fileName, "show2003")) {

			for (int i = 0; i < dbf.getRecordCount(); i++) {
				dbf.read();

				String code = dbf.getField("S1").get();
				String name = dbf.getField("S2").get();

				Stock stock = new Stock();
				stock.setCode(code);
				stock.setName(name);
				stock.setSctype(Stock.SC_SHANGHAI);

				retList.add(stock);
			}
		} else if (StringUtils.containsIgnoreCase(fileName, "sjshq")) {

			for (int i = 0; i < dbf.getRecordCount(); i++) {
				dbf.read();

				String code = dbf.getField(1).get();
				String name = dbf.getField(2).get();

				Stock stock = new Stock();
				stock.setCode(code);
				stock.setName(name);
				stock.setSctype(Stock.SC_SHENZHEN);

				// System.out.println(stock);

				retList.add(stock);
			}

		}

		return retList;
	}

	public static List<LogCJ> toLogCJList(File file) throws xBaseJException, IOException, ParseException {
		
		int sctype = StringUtils.startsWith(file.getName(), "sh")?Stock.SC_SHANGHAI:Stock.SC_SHENZHEN;
		String code = StringUtils.substring(file.getName(), 2,8);
		String dateStr = StringUtils.substringBetween(file.getName(), "_", ".");
				
		List<LogCJ> retList = null;
		
		String encoding = "gbk";
		String s = FileUtils.readFileToString(file, encoding);
		if( StringUtils.isNotBlank(s) )
		{
			String tempS = StringUtils.removeStart(s, "[");
			tempS = StringUtils.removeStart(tempS, "]");
			
			String[] sArray = StringUtils.split(tempS, "\n");
						
			retList = new ArrayList<LogCJ>();
			
			for( int i = 1; i < sArray.length; i++ )
			{
				if( StringUtils.isNotBlank(sArray[i]) )
				{
					String[] tempArray = StringUtils.split(sArray[i],"	");
					String time = tempArray[0];
					float price = NumberUtils.toFloat(tempArray[1]);
					int count = NumberUtils.toInt(tempArray[3]);
					int priceCount = NumberUtils.toInt(tempArray[4]);
					int mmp = 0;
					
					if( tempArray.length >= 6 )
					{
						if( StringUtils.equals(tempArray[5], "买盘") )
						{
							mmp = 1;
						}
						else if( StringUtils.equals(tempArray[5], "卖盘") )
						{
							mmp = 2;
						}
						else if( StringUtils.equals(tempArray[5], "中性盘") )
						{
							mmp = 3;
						}
					}
					
					LogCJ logCJ = new LogCJ();
					logCJ.setCode(code);
					logCJ.setCount(count);
					logCJ.setMmp(mmp);
					logCJ.setPrice(price);
					logCJ.setPriceCount(priceCount);
					logCJ.setSctype(sctype);
					
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					try
					{
						logCJ.setCdate(sdf.parse(dateStr + " " + time));
					}
					catch( Exception e )
					{
						sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
						logCJ.setCdate(sdf.parse(dateStr + " " + time));
					}
					//System.out.println( logCJ );
					
					retList.add(logCJ);
				}
			}
		}
		
		return retList;
	}
	
	
	
	
	public static void main( String[] args ) throws xBaseJException, IOException, ParseException
	{
		File file = new File("d:\\sz300353tdetail_2013-01-17.xls");
		toLogCJList(file);
	}
}
